package com.kiku.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kiku.reggie.common.R;
import com.kiku.reggie.common.SMSUtils;
import com.kiku.reggie.common.SMSUtils1;
import com.kiku.reggie.common.ValidateCodeUtils;
import com.kiku.reggie.entity.User;
import com.kiku.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @className: UserController
 * @description: TODO 类描述
 * @author: kiku
 * @date: 2023/10/22 19:19
 **/
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    //发送手机短信验证码
    @PostMapping("/sendMsg")
    public R<String> sendMsg(HttpSession httpSession, @RequestBody User user) {
        //获取手机号
        String phone = user.getPhone();
        if (StringUtils.isNotEmpty(phone)) {
            //生成随机的四位验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("生成的验证码是:{}", code);
            //调用阿里云提供的短信服务完成发送短信
            SMSUtils.sendMessage(phone, code);
//            //需要将生成的验证码保存到session
//            httpSession.setAttribute(phone, code);

            //将生成的验证码缓存到redis中，并且设置有效期是五分钟
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);

            return R.success("验证码发送成功");
        }

        return R.error("验证码发送失败");
    }


    //用于移动端用户登录
    @PostMapping("/login")
    public R<User> login(HttpSession httpSession, @RequestBody Map user) {
        log.info(user.toString());
        //获取手机号
        String phone = user.get("phone").toString();
        //获取验证码
        String code = user.get("code").toString();
//        //得到session中保存的验证码
//        String sessionCode = (String) httpSession.getAttribute(phone);

        //从redis里面取出验证码
        String sessionCode = (String) redisTemplate.opsForValue().get(phone);
        //进行验证码的比对
        if (sessionCode != null && sessionCode.equals(code)) {
            //如果能够比对成功，则说明登陆成功
            //判断当前手机号对应的用户是否为新用户，如果是新用户就自动完成注册
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getPhone, phone);
            User one = userService.getOne(queryWrapper);
            if (one == null) {
                //由此可知是一个新用户，故完成自动注册
                one = new User();
                one.setPhone(phone);
                userService.save(one);
            }
            httpSession.setAttribute("user", one.getId());
            //如果用户登陆成功,删除redis里缓存的验证码
            redisTemplate.delete(phone);
            return R.success(one);
        }
        return R.error("验证码发送失败");
    }
}
